package com.uuah.server.config.spring.parsers.specific;

import org.springframework.beans.factory.config.BeanDefinition;
import org.w3c.dom.Element;

import com.uuah.config.object.AbstractObjectFactory;
import com.uuah.server.config.spring.parsers.AbstractChildDefinitionParser;
import com.uuah.server.config.spring.parsers.AbstractUuahBeanDefinitionParser;

public class ObjectFactoryDefinitionParser extends
		AbstractChildDefinitionParser {

	protected Class beanClass = null;
	protected String setterMethod = null;

	public ObjectFactoryDefinitionParser(Class beanClass, String setterMethod) {
		this(beanClass);
		this.setterMethod = setterMethod;
	}

	public ObjectFactoryDefinitionParser(Class beanClass) {
		super();
		this.beanClass = beanClass;
		setAllowClassAttribute(false);
		addAlias(AbstractUuahBeanDefinitionParser.ATTRIBUTE_CLASS,
				AbstractObjectFactory.ATTRIBUTE_OBJECT_CLASS_NAME);
		addAlias(AbstractUuahBeanDefinitionParser.ATTRIBUTE_REF, "factoryBean");
	}

	public String getPropertyName(Element element) {
		if (setterMethod != null) {
			return setterMethod;
		} else {
			BeanDefinition parent = getParentBeanDefinition(element);
			String setter = (String) parent
					.getAttribute(ObjectFactoryWrapper.OBJECT_FACTORY_SETTER);
			return setter;
		}
	}

	protected Class getBeanClass(Element element) {
		return beanClass;
	}
}
